const pomelo = require('pomelo');
const routeMgr = require('./app/domain/route');

/**
 * Init app for client.
 */
const app = pomelo.createApp();
app.set('name', 'game-server');



app.loadConfig("mysql", app.getBase() + "/config/mysql/mysql.json"); 
app.loadConfig("redis", app.getBase() + "/config/redis/redis.json"); 

// app configuration
app.route('hall', 	routeMgr.hallRoute);
app.route('ycmj', 	routeMgr.ycmjRoute);

app.configure('production|development',async ()=>{
  const dbclient = require('./app/dao/mysql/mysql').init(app);
  app.set('dbclient', dbclient); 
  
  const redis_db = require('./app/dao/redis/redis_db')(app);
  app.set('redis_db', redis_db);


  const serverType = app.serverType;
  
  if(serverType == 'ycmj'){
    let roomMgr = require('./app/domain/game/roomMgr')({
      gameId:10001,
      time:1000
    })
    console.log('roomMgr--->',roomMgr);
    app.set('roomMgr',roomMgr)
  }
  if(serverType != 'master' && serverType != 'connector'){
    const servers = app.get('servers');
    console.log('serverType---->',serverType,'servers---->',servers);
    for(let server in servers){
      if(server != 'master' && server != 'connector'){
        let serverAry = servers[server];
        console.log('serverAry',serverAry);
        let serverInfo = {
          serverType:serverType
        };
        serverAry.forEach(item => {
          serverInfo[item.id] = 0;
        });
        console.log('serverInfo',serverInfo);
        let repley = await redis_db.HMSETAsync(server,serverInfo);
        console.log('repley----->',repley);
      }
    }
  }
});

app.configure('production|development', 'connector', function(){
  app.set('connectorConfig',
  {
    connector : pomelo.connectors.hybridconnector,
    heartbeat : 3,
    useDict : true,
    useProtobuf : true
  });
  app.loadConfig("app_sceret", app.getBase() + "/config/jwt.json");
});
app.configure('production|development', 'hall', function(){
 
});
app.configure('production|development', 'ycmj', function(){
 
});

// start app
app.start();

process.on('uncaughtException', function (err) {
  console.error(' Caught exception: ' + err.stack);
});
